import * as _ from "lodash"
import httpClient from "../httpclient"

export class CustomCollection {
  public id: string
  public name: string
  public products_count: number
  public created_time: Date
  public position: number

  public get productsCount() {
    return this.products_count
  }

  public get createdTime() {
    return this.created_time
  }
}

export class CollectionServiceV1 {

  public static createCollection(storeId: string, collection: CustomCollection): Promise<CustomCollection> {
    return httpClient.post(`/v1/stores/${storeId}/collections`, collection)
      .then(({ data }) => _.assign(new CustomCollection(), data))
  }

  public static deleteCollection(storeId: string, collectionId: string): Promise<void> {
    return httpClient.delete(`/v1/stores/${storeId}/collections/${collectionId}`)
  }

  public static saveCollection(storeId: string, collection: CustomCollection): Promise<void> {
    return httpClient.put(`/v1/stores/${storeId}/collections/${collection.id}`, collection)
  }

  public static getCollections(storeId: string): Promise<CustomCollection[]> {
    return httpClient.get(`/v1/stores/${storeId}/collections`).then(({ data: collections }) =>
      _.map(collections, customCollection => _.assign(new CustomCollection(), customCollection)))
  }
}
